

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.models.bist.eval.conllu.conll17_ud_eval &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../../../" src="../../../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../../../index.html">
          

          
            
            <img src="../../../../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../../../../models.html">nlp_architect.models</a> &raquo;</li>
        
      <li>nlp_architect.models.bist.eval.conllu.conll17_ud_eval</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.models.bist.eval.conllu.conll17_ud_eval</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2018 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="c1"># Things that were changed from the original:</span>
<span class="c1"># - Added legal header</span>
<span class="c1"># - Reformatted code and variable names to conform with PEP8</span>
<span class="c1"># - Added pointer to &#39;weights.clas&#39; file</span>
<span class="c1"># - Added run_conllu_eval()</span>
<span class="c1"># - Removed tests and command-line usage option</span>
<span class="c1"># - Removed unnecessary imports</span>
<span class="c1"># - Add pylint check disable flags</span>

<span class="c1"># !/usr/bin/env python</span>
<span class="c1"># CoNLL 2017 UD Parsing evaluation script.</span>
<span class="c1">#</span>
<span class="c1"># Compatible with Python 2.7 and 3.2+, can be used either as a module</span>
<span class="c1"># or a standalone executable.</span>
<span class="c1">#</span>
<span class="c1"># Copyright 2017 Institute of Formal and Applied Linguistics (UFAL),</span>
<span class="c1"># Faculty of Mathematics and Physics, Charles University, Czech Republic.</span>
<span class="c1">#</span>
<span class="c1"># Changelog:</span>
<span class="c1"># - [02 Jan 2017] Version 0.9: Initial release</span>
<span class="c1"># - [25 Jan 2017] Version 0.9.1: Fix bug in LCS alignment computation</span>
<span class="c1"># - [10 Mar 2017] Version 1.0: Add documentation and test</span>
<span class="c1">#                              Compare HEADs correctly using aligned words</span>
<span class="c1">#                              Allow evaluation with errorneous spaces in forms</span>
<span class="c1">#                              Compare forms in LCS case insensitively</span>
<span class="c1">#                              Detect cycles and multiple root nodes</span>
<span class="c1">#                              Compute AlignedAccuracy</span>
<span class="c1"># API usage</span>
<span class="c1"># ---------</span>
<span class="c1"># - load_conllu(file)</span>
<span class="c1">#   - loads CoNLL-U file from given file object to an internal representation</span>
<span class="c1">#   - the file object should return str on both Python 2 and Python 3</span>
<span class="c1">#   - raises UDError exception if the given file cannot be loaded</span>
<span class="c1"># - evaluate(gold_ud, system_ud)</span>
<span class="c1">#   - evaluate the given gold and system CoNLL-U files (loaded with</span>
<span class="c1">#     load_conllu)</span>
<span class="c1">#   - raises UDError if the concatenated tokens of gold and system file do not</span>
<span class="c1">#     match</span>
<span class="c1">#   - returns a dictionary with the metrics described above, each metrics</span>
<span class="c1">#     having three fields: precision, recall and f1</span>
<span class="c1">#</span>
<span class="c1"># Description of token matching</span>
<span class="c1"># -----------------------------</span>
<span class="c1"># In order to match tokens of gold file and system file, we consider the text</span>
<span class="c1"># resulting from concatenation of gold tokens and text resulting from</span>
<span class="c1"># concatenation of system tokens. These texts should match -- if they do not,</span>
<span class="c1"># the evaluation fails.</span>
<span class="c1">#</span>
<span class="c1"># If the texts do match, every token is represented as a range in this original</span>
<span class="c1"># text, and tokens are equal only if their range is the same.</span>
<span class="c1">#</span>
<span class="c1"># Description of word matching</span>
<span class="c1"># ----------------------------</span>
<span class="c1"># When matching words of gold file and system file, we first match the tokens.</span>
<span class="c1"># The words which are also tokens are matched as tokens, but words in</span>
<span class="c1"># multi-word tokens have to be handled differently.</span>
<span class="c1">#</span>
<span class="c1"># To handle multi-word tokens, we start by finding &quot;multi-word spans&quot;.</span>
<span class="c1"># Multi-word span is a span in the original text such that</span>
<span class="c1"># - it contains at least one multi-word token</span>
<span class="c1"># - all multi-word tokens in the span (considering both gold and system ones)</span>
<span class="c1">#   are completely inside the span (i.e., they do not &quot;stick out&quot;)</span>
<span class="c1"># - the multi-word span is as small as possible</span>
<span class="c1">#</span>
<span class="c1"># For every multi-word span, we align the gold and system words completely</span>
<span class="c1"># inside this span using LCS on their FORMs. The words not intersecting</span>
<span class="c1"># (even partially) any multi-word span are then aligned as tokens.</span>
<span class="c1"># pylint: disable=too-many-statements</span>

<span class="c1"># CoNLL-U column names</span>
<span class="n">ID</span><span class="p">,</span> <span class="n">FORM</span><span class="p">,</span> <span class="n">LEMMA</span><span class="p">,</span> <span class="n">UPOS</span><span class="p">,</span> <span class="n">XPOS</span><span class="p">,</span> <span class="n">FEATS</span><span class="p">,</span> <span class="n">HEAD</span><span class="p">,</span> <span class="n">DEPREL</span><span class="p">,</span> <span class="n">DEPS</span><span class="p">,</span> <span class="n">MISC</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>

<span class="n">WEIGHTS</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)),</span> <span class="s2">&quot;weights.clas&quot;</span><span class="p">)</span>


<span class="c1"># UD Error is used when raising exceptions in this module</span>
<div class="viewcode-block" id="UDError"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.UDError">[docs]</a><span class="k">class</span> <span class="nc">UDError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span></div>


<span class="c1"># Load given CoNLL-U file into internal representation</span>
<div class="viewcode-block" id="load_conllu"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.load_conllu">[docs]</a><span class="k">def</span> <span class="nf">load_conllu</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
    <span class="c1"># pylint: disable=too-many-locals</span>
    <span class="c1"># pylint: disable=too-many-branches</span>
    <span class="c1"># pylint: disable=too-many-statements</span>

    <span class="c1"># Internal representation classes</span>
    <span class="k">class</span> <span class="nc">UDRepresentation</span><span class="p">:</span>
        <span class="c1"># pylint: disable=too-few-public-methods</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="c1"># Characters of all the tokens in the whole file.</span>
            <span class="c1"># Whitespace between tokens is not included.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">characters</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="c1"># List of UDSpan instances with start&amp;end indices into `characters`</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">tokens</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="c1"># List of UDWord instances.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">words</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="c1"># List of UDSpan instances with start&amp;end indices into `characters`</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sentences</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">class</span> <span class="nc">UDSpan</span><span class="p">:</span>
        <span class="c1"># pylint: disable=too-few-public-methods</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">start</span>
            <span class="c1"># Note that self.end marks the first position **after the end** of</span>
            <span class="c1"># span, so we can use characters[start:end] or range(start, end).</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">end</span> <span class="o">=</span> <span class="n">end</span>

    <span class="k">class</span> <span class="nc">UDWord</span><span class="p">:</span>
        <span class="c1"># pylint: disable=too-few-public-methods</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">span</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">is_multiword</span><span class="p">):</span>
            <span class="c1"># Span of this word (or MWT, see below) within</span>
            <span class="c1"># ud_representation.characters.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">span</span> <span class="o">=</span> <span class="n">span</span>
            <span class="c1"># 10 columns of the CoNLL-U file: ID, FORM, LEMMA,...</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="n">columns</span>
            <span class="c1"># is_multiword==True means that this word is part of a multi-word</span>
            <span class="c1"># token.</span>
            <span class="c1"># In that case, self.span marks the span of the whole multi-word</span>
            <span class="c1"># token.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">is_multiword</span> <span class="o">=</span> <span class="n">is_multiword</span>
            <span class="c1"># Reference to the UDWord instance representing the HEAD (or None</span>
            <span class="c1"># if root).</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="c1"># Let&#39;s ignore language-specific deprel subtypes.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">DEPREL</span><span class="p">]</span> <span class="o">=</span> <span class="n">columns</span><span class="p">[</span><span class="n">DEPREL</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

    <span class="n">ud</span> <span class="o">=</span> <span class="n">UDRepresentation</span><span class="p">()</span>

    <span class="c1"># Load the CoNLL-U file</span>
    <span class="n">index</span><span class="p">,</span> <span class="n">sentence_start</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="kc">None</span>
    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
        <span class="n">line</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span>
            <span class="k">break</span>
        <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\r\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Handle sentence start boundaries</span>
        <span class="k">if</span> <span class="n">sentence_start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Skip comments</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;#&quot;</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="c1"># Start a new sentence</span>
            <span class="n">ud</span><span class="o">.</span><span class="n">sentences</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">UDSpan</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
            <span class="n">sentence_start</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="p">:</span>
            <span class="c1"># Add parent UDWord links and check there are no cycles</span>
            <span class="k">def</span> <span class="nf">process_word</span><span class="p">(</span><span class="n">word</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">word</span><span class="o">.</span><span class="n">parent</span> <span class="o">==</span> <span class="s2">&quot;remapping&quot;</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;There is a cycle in a sentence&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">word</span><span class="o">.</span><span class="n">parent</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">head</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">word</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">HEAD</span><span class="p">])</span>
                    <span class="k">if</span> <span class="n">head</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">)</span> <span class="o">-</span> <span class="n">sentence_start</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span>
                            <span class="s2">&quot;HEAD &#39;</span><span class="si">{}</span><span class="s2">&#39; points outside of the sentence&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">word</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">HEAD</span><span class="p">])</span>
                        <span class="p">)</span>
                    <span class="k">if</span> <span class="n">head</span><span class="p">:</span>
                        <span class="n">parent</span> <span class="o">=</span> <span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">[</span><span class="n">sentence_start</span> <span class="o">+</span> <span class="n">head</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
                        <span class="n">word</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="s2">&quot;remapping&quot;</span>
                        <span class="n">process_word</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>
                        <span class="n">word</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span>

            <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">[</span><span class="n">sentence_start</span><span class="p">:]:</span>
                <span class="n">process_word</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>

            <span class="c1"># Check there is a single root node</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">([</span><span class="n">word</span> <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">[</span><span class="n">sentence_start</span><span class="p">:]</span> <span class="k">if</span> <span class="n">word</span><span class="o">.</span><span class="n">parent</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">])</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;There are multiple roots in a sentence&quot;</span><span class="p">)</span>

            <span class="c1"># End the sentence</span>
            <span class="n">ud</span><span class="o">.</span><span class="n">sentences</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">end</span> <span class="o">=</span> <span class="n">index</span>
            <span class="n">sentence_start</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">continue</span>

        <span class="c1"># Read next token/word</span>
        <span class="n">columns</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">10</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span>
                <span class="s2">&quot;The CoNLL-U line does not contain 10 tab-separated columns: &quot;</span> <span class="s2">&quot;&#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="c1"># Skip empty nodes</span>
        <span class="k">if</span> <span class="s2">&quot;.&quot;</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">]:</span>
            <span class="k">continue</span>

        <span class="c1"># Delete spaces from FORM  so gold.characters == system.characters</span>
        <span class="c1"># even if one of them tokenizes the space.</span>
        <span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">]</span> <span class="o">=</span> <span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;There is an empty FORM in the CoNLL-U file&quot;</span><span class="p">)</span>

        <span class="c1"># Save token</span>
        <span class="n">ud</span><span class="o">.</span><span class="n">characters</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">])</span>
        <span class="n">ud</span><span class="o">.</span><span class="n">tokens</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">UDSpan</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">index</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">])))</span>
        <span class="n">index</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">])</span>

        <span class="c1"># Handle multi-word tokens to save word(s)</span>
        <span class="k">if</span> <span class="s2">&quot;-&quot;</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">]:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)))</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;Cannot parse multi-word token ID &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">]))</span>

            <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">word_line</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\r\n</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="n">word_columns</span> <span class="o">=</span> <span class="n">word_line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">word_columns</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">10</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span>
                        <span class="s2">&quot;The CoNLL-U line does not contain 10 tab-separated &quot;</span>
                        <span class="s2">&quot;columns: &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">word_line</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">UDWord</span><span class="p">(</span><span class="n">ud</span><span class="o">.</span><span class="n">tokens</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">word_columns</span><span class="p">,</span> <span class="n">is_multiword</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
        <span class="c1"># Basic tokens/words</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">word_id</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">])</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;Cannot parse word ID &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">]))</span>
            <span class="k">if</span> <span class="n">word_id</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">)</span> <span class="o">-</span> <span class="n">sentence_start</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span>
                    <span class="s2">&quot;Incorrect word ID &#39;</span><span class="si">{}</span><span class="s2">&#39; for word &#39;</span><span class="si">{}</span><span class="s2">&#39;, expected&quot;</span>
                    <span class="s2">&quot; &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">ID</span><span class="p">],</span> <span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">],</span> <span class="nb">len</span><span class="p">(</span><span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="p">)</span> <span class="o">-</span> <span class="n">sentence_start</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
                <span class="p">)</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">head_id</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">HEAD</span><span class="p">])</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;Cannot parse HEAD &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="n">HEAD</span><span class="p">]))</span>
            <span class="k">if</span> <span class="n">head_id</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;HEAD cannot be negative&quot;</span><span class="p">)</span>

            <span class="n">ud</span><span class="o">.</span><span class="n">words</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">UDWord</span><span class="p">(</span><span class="n">ud</span><span class="o">.</span><span class="n">tokens</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">columns</span><span class="p">,</span> <span class="n">is_multiword</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">sentence_start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span><span class="s2">&quot;The CoNLL-U file does not end with empty line&quot;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">ud</span></div>


<span class="c1"># Evaluate the gold and system treebanks (loaded using load_conllu).</span>
<div class="viewcode-block" id="evaluate"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.evaluate">[docs]</a><span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="n">gold_ud</span><span class="p">,</span> <span class="n">system_ud</span><span class="p">,</span> <span class="n">deprel_weights</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="c1"># pylint: disable=too-many-locals</span>
    <span class="k">class</span> <span class="nc">Score</span><span class="p">:</span>
        <span class="c1"># pylint: disable=too-few-public-methods</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gold_total</span><span class="p">,</span> <span class="n">system_total</span><span class="p">,</span> <span class="n">correct</span><span class="p">,</span> <span class="n">aligned_total</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">precision</span> <span class="o">=</span> <span class="n">correct</span> <span class="o">/</span> <span class="n">system_total</span> <span class="k">if</span> <span class="n">system_total</span> <span class="k">else</span> <span class="mf">0.0</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">recall</span> <span class="o">=</span> <span class="n">correct</span> <span class="o">/</span> <span class="n">gold_total</span> <span class="k">if</span> <span class="n">gold_total</span> <span class="k">else</span> <span class="mf">0.0</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">f1</span> <span class="o">=</span> <span class="p">(</span>
                <span class="mi">2</span> <span class="o">*</span> <span class="n">correct</span> <span class="o">/</span> <span class="p">(</span><span class="n">system_total</span> <span class="o">+</span> <span class="n">gold_total</span><span class="p">)</span> <span class="k">if</span> <span class="n">system_total</span> <span class="o">+</span> <span class="n">gold_total</span> <span class="k">else</span> <span class="mf">0.0</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">aligned_accuracy</span> <span class="o">=</span> <span class="n">correct</span> <span class="o">/</span> <span class="n">aligned_total</span> <span class="k">if</span> <span class="n">aligned_total</span> <span class="k">else</span> <span class="n">aligned_total</span>

    <span class="k">class</span> <span class="nc">AlignmentWord</span><span class="p">:</span>
        <span class="c1"># pylint: disable=too-few-public-methods</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gold_word</span><span class="p">,</span> <span class="n">system_word</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gold_word</span> <span class="o">=</span> <span class="n">gold_word</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">system_word</span> <span class="o">=</span> <span class="n">system_word</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gold_parent</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">system_parent_gold_aligned</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">class</span> <span class="nc">Alignment</span><span class="p">:</span>
        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">gold_words</span> <span class="o">=</span> <span class="n">gold_words</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">system_words</span> <span class="o">=</span> <span class="n">system_words</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">matched_words</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">matched_words_map</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">def</span> <span class="nf">append_aligned_words</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gold_word</span><span class="p">,</span> <span class="n">system_word</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">matched_words</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">AlignmentWord</span><span class="p">(</span><span class="n">gold_word</span><span class="p">,</span> <span class="n">system_word</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">matched_words_map</span><span class="p">[</span><span class="n">system_word</span><span class="p">]</span> <span class="o">=</span> <span class="n">gold_word</span>

        <span class="k">def</span> <span class="nf">fill_parents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="c1"># We represent root parents in both gold and system data by &#39;0&#39;.</span>
            <span class="c1"># For gold data, we represent non-root parent by corresponding gold</span>
            <span class="c1"># word.</span>
            <span class="c1"># For system data, we represent non-root parent by either gold word</span>
            <span class="c1"># aligned</span>
            <span class="c1"># to parent system nodes, or by None if no gold words is aligned to</span>
            <span class="c1"># the parent.</span>
            <span class="k">for</span> <span class="n">words</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">matched_words</span><span class="p">:</span>
                <span class="n">words</span><span class="o">.</span><span class="n">gold_parent</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">words</span><span class="o">.</span><span class="n">gold_word</span><span class="o">.</span><span class="n">parent</span> <span class="k">if</span> <span class="n">words</span><span class="o">.</span><span class="n">gold_word</span><span class="o">.</span><span class="n">parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="mi">0</span>
                <span class="p">)</span>
                <span class="n">words</span><span class="o">.</span><span class="n">system_parent_gold_aligned</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">matched_words_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">words</span><span class="o">.</span><span class="n">system_word</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">words</span><span class="o">.</span><span class="n">system_word</span><span class="o">.</span><span class="n">parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
                    <span class="k">else</span> <span class="mi">0</span>
                <span class="p">)</span>

    <span class="k">def</span> <span class="nf">lower</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">text</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">text</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">spans_score</span><span class="p">(</span><span class="n">gold_spans</span><span class="p">,</span> <span class="n">system_spans</span><span class="p">):</span>
        <span class="n">correct</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">gi</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">gold_spans</span><span class="p">)</span> <span class="ow">and</span> <span class="n">si</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">system_spans</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">system_spans</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">start</span> <span class="o">&lt;</span> <span class="n">gold_spans</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">start</span><span class="p">:</span>
                <span class="n">si</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="n">gold_spans</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">start</span> <span class="o">&lt;</span> <span class="n">system_spans</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">start</span><span class="p">:</span>
                <span class="n">gi</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">correct</span> <span class="o">+=</span> <span class="n">gold_spans</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">end</span> <span class="o">==</span> <span class="n">system_spans</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">end</span>
                <span class="n">si</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">gi</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">Score</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">gold_spans</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">system_spans</span><span class="p">),</span> <span class="n">correct</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="n">key_fn</span><span class="p">,</span> <span class="n">weight_fn</span><span class="o">=</span><span class="k">lambda</span> <span class="n">w</span><span class="p">:</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">gold</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">aligned</span><span class="p">,</span> <span class="n">correct</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">alignment</span><span class="o">.</span><span class="n">gold_words</span><span class="p">:</span>
            <span class="n">gold</span> <span class="o">+=</span> <span class="n">weight_fn</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">alignment</span><span class="o">.</span><span class="n">system_words</span><span class="p">:</span>
            <span class="n">system</span> <span class="o">+=</span> <span class="n">weight_fn</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">words</span> <span class="ow">in</span> <span class="n">alignment</span><span class="o">.</span><span class="n">matched_words</span><span class="p">:</span>
            <span class="n">aligned</span> <span class="o">+=</span> <span class="n">weight_fn</span><span class="p">(</span><span class="n">words</span><span class="o">.</span><span class="n">gold_word</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">key_fn</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Return score for whole aligned words</span>
            <span class="k">return</span> <span class="n">Score</span><span class="p">(</span><span class="n">gold</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">aligned</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">words</span> <span class="ow">in</span> <span class="n">alignment</span><span class="o">.</span><span class="n">matched_words</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key_fn</span><span class="p">(</span><span class="n">words</span><span class="o">.</span><span class="n">gold_word</span><span class="p">,</span> <span class="n">words</span><span class="o">.</span><span class="n">gold_parent</span><span class="p">)</span> <span class="o">==</span> <span class="n">key_fn</span><span class="p">(</span>
                <span class="n">words</span><span class="o">.</span><span class="n">system_word</span><span class="p">,</span> <span class="n">words</span><span class="o">.</span><span class="n">system_parent_gold_aligned</span>
            <span class="p">):</span>
                <span class="n">correct</span> <span class="o">+=</span> <span class="n">weight_fn</span><span class="p">(</span><span class="n">words</span><span class="o">.</span><span class="n">gold_word</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">Score</span><span class="p">(</span><span class="n">gold</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">correct</span><span class="p">,</span> <span class="n">aligned</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">beyond_end</span><span class="p">(</span><span class="n">words</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">multiword_span_end</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">words</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">is_multiword</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span> <span class="o">&gt;=</span> <span class="n">multiword_span_end</span>
        <span class="k">return</span> <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span> <span class="o">&gt;</span> <span class="n">multiword_span_end</span>

    <span class="k">def</span> <span class="nf">extend_end</span><span class="p">(</span><span class="n">word</span><span class="p">,</span> <span class="n">multiword_span_end</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">word</span><span class="o">.</span><span class="n">is_multiword</span> <span class="ow">and</span> <span class="n">word</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span> <span class="o">&gt;</span> <span class="n">multiword_span_end</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">word</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span>
        <span class="k">return</span> <span class="n">multiword_span_end</span>

    <span class="k">def</span> <span class="nf">find_multiword_span</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span><span class="p">):</span>
        <span class="c1"># We know gold_words[gi].is_multiword or system_words[si].is_multiword.</span>
        <span class="c1"># Find the start of the multiword span (gs, ss), so the multiword span</span>
        <span class="c1"># is minimal.</span>
        <span class="c1"># Initialize multiword_span_end characters index.</span>
        <span class="k">if</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">is_multiword</span><span class="p">:</span>
            <span class="n">multiword_span_end</span> <span class="o">=</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span>
            <span class="k">if</span> <span class="p">(</span>
                <span class="ow">not</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">is_multiword</span>
                <span class="ow">and</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span> <span class="o">&lt;</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span>
            <span class="p">):</span>
                <span class="n">si</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>  <span class="c1"># if system_words[si].is_multiword</span>
            <span class="n">multiword_span_end</span> <span class="o">=</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span>
            <span class="k">if</span> <span class="p">(</span>
                <span class="ow">not</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">is_multiword</span>
                <span class="ow">and</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span> <span class="o">&lt;</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span>
            <span class="p">):</span>
                <span class="n">gi</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">gs</span><span class="p">,</span> <span class="n">ss</span> <span class="o">=</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span>

        <span class="c1"># Find the end of the multiword span</span>
        <span class="c1"># (so both gi and si are pointing to the word following the multiword</span>
        <span class="c1"># span end).</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="n">beyond_end</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">multiword_span_end</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">beyond_end</span><span class="p">(</span>
            <span class="n">system_words</span><span class="p">,</span> <span class="n">si</span><span class="p">,</span> <span class="n">multiword_span_end</span>
        <span class="p">):</span>
            <span class="n">gold_start</span> <span class="o">=</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span>
            <span class="n">sys_start</span> <span class="o">=</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span>
            <span class="k">if</span> <span class="n">gi</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">gold_words</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">si</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">system_words</span><span class="p">)</span> <span class="ow">or</span> <span class="n">gold_start</span> <span class="o">&lt;=</span> <span class="n">sys_start</span><span class="p">):</span>
                <span class="n">multiword_span_end</span> <span class="o">=</span> <span class="n">extend_end</span><span class="p">(</span><span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">],</span> <span class="n">multiword_span_end</span><span class="p">)</span>
                <span class="n">gi</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">multiword_span_end</span> <span class="o">=</span> <span class="n">extend_end</span><span class="p">(</span><span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">],</span> <span class="n">multiword_span_end</span><span class="p">)</span>
                <span class="n">si</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">gs</span><span class="p">,</span> <span class="n">ss</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span>

    <span class="k">def</span> <span class="nf">compute_lcs</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span><span class="p">,</span> <span class="n">gs</span><span class="p">,</span> <span class="n">ss</span><span class="p">):</span>
        <span class="c1"># pylint: disable=too-many-arguments</span>
        <span class="n">lcs</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">si</span> <span class="o">-</span> <span class="n">ss</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">gi</span> <span class="o">-</span> <span class="n">gs</span><span class="p">)]</span>
        <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">gi</span> <span class="o">-</span> <span class="n">gs</span><span class="p">))):</span>
            <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">si</span> <span class="o">-</span> <span class="n">ss</span><span class="p">))):</span>
                <span class="k">if</span> <span class="n">lower</span><span class="p">(</span><span class="n">gold_words</span><span class="p">[</span><span class="n">gs</span> <span class="o">+</span> <span class="n">g</span><span class="p">]</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">])</span> <span class="o">==</span> <span class="n">lower</span><span class="p">(</span>
                    <span class="n">system_words</span><span class="p">[</span><span class="n">ss</span> <span class="o">+</span> <span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">]</span>
                <span class="p">):</span>
                    <span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="p">(</span>
                        <span class="n">lcs</span><span class="p">[</span><span class="n">g</span> <span class="o">+</span> <span class="mi">1</span><span class="p">][</span><span class="n">s</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="n">g</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">gi</span> <span class="o">-</span> <span class="n">gs</span> <span class="ow">and</span> <span class="n">s</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">si</span> <span class="o">-</span> <span class="n">ss</span> <span class="k">else</span> <span class="mi">0</span>
                    <span class="p">)</span>
                <span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span><span class="p">],</span> <span class="n">lcs</span><span class="p">[</span><span class="n">g</span> <span class="o">+</span> <span class="mi">1</span><span class="p">][</span><span class="n">s</span><span class="p">]</span> <span class="k">if</span> <span class="n">g</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">gi</span> <span class="o">-</span> <span class="n">gs</span> <span class="k">else</span> <span class="mi">0</span><span class="p">)</span>
                <span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span><span class="p">],</span> <span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="n">s</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">si</span> <span class="o">-</span> <span class="n">ss</span> <span class="k">else</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">lcs</span>

    <span class="k">def</span> <span class="nf">align_words</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">):</span>
        <span class="n">alignment</span> <span class="o">=</span> <span class="n">Alignment</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">)</span>

        <span class="n">gi</span><span class="p">,</span> <span class="n">si</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">gi</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">gold_words</span><span class="p">)</span> <span class="ow">and</span> <span class="n">si</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">system_words</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">is_multiword</span> <span class="ow">or</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">is_multiword</span><span class="p">:</span>
                <span class="c1"># A: Multi-word tokens =&gt; align via LCS within the whole</span>
                <span class="c1"># &quot;multiword span&quot;.</span>
                <span class="n">gs</span><span class="p">,</span> <span class="n">ss</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span> <span class="o">=</span> <span class="n">find_multiword_span</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">si</span> <span class="o">&gt;</span> <span class="n">ss</span> <span class="ow">and</span> <span class="n">gi</span> <span class="o">&gt;</span> <span class="n">gs</span><span class="p">:</span>
                    <span class="n">lcs</span> <span class="o">=</span> <span class="n">compute_lcs</span><span class="p">(</span><span class="n">gold_words</span><span class="p">,</span> <span class="n">system_words</span><span class="p">,</span> <span class="n">gi</span><span class="p">,</span> <span class="n">si</span><span class="p">,</span> <span class="n">gs</span><span class="p">,</span> <span class="n">ss</span><span class="p">)</span>

                    <span class="c1"># Store aligned words</span>
                    <span class="n">s</span><span class="p">,</span> <span class="n">g</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
                    <span class="k">while</span> <span class="n">g</span> <span class="o">&lt;</span> <span class="n">gi</span> <span class="o">-</span> <span class="n">gs</span> <span class="ow">and</span> <span class="n">s</span> <span class="o">&lt;</span> <span class="n">si</span> <span class="o">-</span> <span class="n">ss</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">lower</span><span class="p">(</span><span class="n">gold_words</span><span class="p">[</span><span class="n">gs</span> <span class="o">+</span> <span class="n">g</span><span class="p">]</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">])</span> <span class="o">==</span> <span class="n">lower</span><span class="p">(</span>
                            <span class="n">system_words</span><span class="p">[</span><span class="n">ss</span> <span class="o">+</span> <span class="n">s</span><span class="p">]</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">FORM</span><span class="p">]</span>
                        <span class="p">):</span>
                            <span class="n">alignment</span><span class="o">.</span><span class="n">append_aligned_words</span><span class="p">(</span><span class="n">gold_words</span><span class="p">[</span><span class="n">gs</span> <span class="o">+</span> <span class="n">g</span><span class="p">],</span> <span class="n">system_words</span><span class="p">[</span><span class="n">ss</span> <span class="o">+</span> <span class="n">s</span><span class="p">])</span>
                            <span class="n">g</span> <span class="o">+=</span> <span class="mi">1</span>
                            <span class="n">s</span> <span class="o">+=</span> <span class="mi">1</span>
                        <span class="k">elif</span> <span class="n">lcs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">s</span><span class="p">]</span> <span class="o">==</span> <span class="p">(</span><span class="n">lcs</span><span class="p">[</span><span class="n">g</span> <span class="o">+</span> <span class="mi">1</span><span class="p">][</span><span class="n">s</span><span class="p">]</span> <span class="k">if</span> <span class="n">g</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">gi</span> <span class="o">-</span> <span class="n">gs</span> <span class="k">else</span> <span class="mi">0</span><span class="p">):</span>
                            <span class="n">g</span> <span class="o">+=</span> <span class="mi">1</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">s</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># B: No multi-word token =&gt; align according to spans.</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span><span class="p">,</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span>
                    <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span><span class="p">,</span>
                    <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">end</span><span class="p">,</span>
                <span class="p">):</span>
                    <span class="n">alignment</span><span class="o">.</span><span class="n">append_aligned_words</span><span class="p">(</span><span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">],</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">])</span>
                    <span class="n">gi</span> <span class="o">+=</span> <span class="mi">1</span>
                    <span class="n">si</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">elif</span> <span class="n">gold_words</span><span class="p">[</span><span class="n">gi</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span> <span class="o">&lt;=</span> <span class="n">system_words</span><span class="p">[</span><span class="n">si</span><span class="p">]</span><span class="o">.</span><span class="n">span</span><span class="o">.</span><span class="n">start</span><span class="p">:</span>
                    <span class="n">gi</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">si</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="n">alignment</span><span class="o">.</span><span class="n">fill_parents</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">alignment</span>

    <span class="c1"># Check that underlying character sequences do match</span>
    <span class="k">if</span> <span class="n">gold_ud</span><span class="o">.</span><span class="n">characters</span> <span class="o">!=</span> <span class="n">system_ud</span><span class="o">.</span><span class="n">characters</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">gold_ud</span><span class="o">.</span><span class="n">characters</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">==</span> <span class="n">system_ud</span><span class="o">.</span><span class="n">characters</span><span class="p">[</span><span class="n">index</span><span class="p">]:</span>
            <span class="n">index</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">raise</span> <span class="n">UDError</span><span class="p">(</span>
            <span class="s2">&quot;The concatenation of tokens in gold file and in system file &quot;</span>
            <span class="s2">&quot;differ!</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="s2">&quot;First 20 differing characters in gold file: &#39;</span><span class="si">{}</span><span class="s2">&#39; and system file:&quot;</span>
            <span class="s2">&quot; &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">gold_ud</span><span class="o">.</span><span class="n">characters</span><span class="p">[</span><span class="n">index</span> <span class="p">:</span> <span class="n">index</span> <span class="o">+</span> <span class="mi">20</span><span class="p">]),</span>
                <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">system_ud</span><span class="o">.</span><span class="n">characters</span><span class="p">[</span><span class="n">index</span> <span class="p">:</span> <span class="n">index</span> <span class="o">+</span> <span class="mi">20</span><span class="p">]),</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="c1"># Align words</span>
    <span class="n">alignment</span> <span class="o">=</span> <span class="n">align_words</span><span class="p">(</span><span class="n">gold_ud</span><span class="o">.</span><span class="n">words</span><span class="p">,</span> <span class="n">system_ud</span><span class="o">.</span><span class="n">words</span><span class="p">)</span>

    <span class="c1"># Compute the F1-scores</span>
    <span class="n">result</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s2">&quot;Tokens&quot;</span><span class="p">:</span> <span class="n">spans_score</span><span class="p">(</span><span class="n">gold_ud</span><span class="o">.</span><span class="n">tokens</span><span class="p">,</span> <span class="n">system_ud</span><span class="o">.</span><span class="n">tokens</span><span class="p">),</span>
        <span class="s2">&quot;Sentences&quot;</span><span class="p">:</span> <span class="n">spans_score</span><span class="p">(</span><span class="n">gold_ud</span><span class="o">.</span><span class="n">sentences</span><span class="p">,</span> <span class="n">system_ud</span><span class="o">.</span><span class="n">sentences</span><span class="p">),</span>
        <span class="s2">&quot;Words&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
        <span class="s2">&quot;UPOS&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">UPOS</span><span class="p">]),</span>
        <span class="s2">&quot;XPOS&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">XPOS</span><span class="p">]),</span>
        <span class="s2">&quot;Feats&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">FEATS</span><span class="p">]),</span>
        <span class="s2">&quot;AllTags&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span>
            <span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">UPOS</span><span class="p">],</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">XPOS</span><span class="p">],</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">FEATS</span><span class="p">])</span>
        <span class="p">),</span>
        <span class="s2">&quot;Lemmas&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">LEMMA</span><span class="p">]),</span>
        <span class="s2">&quot;UAS&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">parent</span><span class="p">),</span>
        <span class="s2">&quot;LAS&quot;</span><span class="p">:</span> <span class="n">alignment_score</span><span class="p">(</span><span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">DEPREL</span><span class="p">])),</span>
    <span class="p">}</span>

    <span class="c1"># Add WeightedLAS if weights are given</span>
    <span class="k">if</span> <span class="n">deprel_weights</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>

        <span class="k">def</span> <span class="nf">weighted_las</span><span class="p">(</span><span class="n">word</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">deprel_weights</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">word</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">DEPREL</span><span class="p">],</span> <span class="mf">1.0</span><span class="p">)</span>

        <span class="n">result</span><span class="p">[</span><span class="s2">&quot;WeightedLAS&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">alignment_score</span><span class="p">(</span>
            <span class="n">alignment</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">w</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">w</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="n">DEPREL</span><span class="p">]),</span> <span class="n">weighted_las</span>
        <span class="p">)</span>

    <span class="k">return</span> <span class="n">result</span></div>


<div class="viewcode-block" id="load_deprel_weights"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.load_deprel_weights">[docs]</a><span class="k">def</span> <span class="nf">load_deprel_weights</span><span class="p">(</span><span class="n">weights_file</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">weights_file</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="n">deprel_weights</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">weights_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
            <span class="c1"># Ignore comments and empty lines</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;#&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">():</span>
                <span class="k">continue</span>

            <span class="n">columns</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\r\n</span><span class="s2">&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">columns</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Expected two columns in the UD Relations weights file on line&quot;</span>
                    <span class="s2">&quot; &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="p">)</span>

            <span class="n">deprel_weights</span><span class="p">[</span><span class="n">columns</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">columns</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

    <span class="k">return</span> <span class="n">deprel_weights</span></div>


<div class="viewcode-block" id="load_conllu_file"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.load_conllu_file">[docs]</a><span class="k">def</span> <span class="nf">load_conllu_file</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span>
        <span class="n">path</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;r&quot;</span><span class="p">,</span> <span class="o">**</span><span class="p">({</span><span class="s2">&quot;encoding&quot;</span><span class="p">:</span> <span class="s2">&quot;utf-8&quot;</span><span class="p">}</span> <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="k">else</span> <span class="p">{})</span>
    <span class="p">)</span> <span class="k">as</span> <span class="n">_file</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">load_conllu</span><span class="p">(</span><span class="n">_file</span><span class="p">)</span></div>


<div class="viewcode-block" id="evaluate_wrapper"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.evaluate_wrapper">[docs]</a><span class="k">def</span> <span class="nf">evaluate_wrapper</span><span class="p">(</span><span class="n">gold_file</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">system_file</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">weights_file</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
    <span class="c1"># Load CoNLL-U files</span>
    <span class="n">gold_ud</span> <span class="o">=</span> <span class="n">load_conllu_file</span><span class="p">(</span><span class="n">gold_file</span><span class="p">)</span>
    <span class="n">system_ud</span> <span class="o">=</span> <span class="n">load_conllu_file</span><span class="p">(</span><span class="n">system_file</span><span class="p">)</span>

    <span class="c1"># Load weights if requested</span>
    <span class="n">deprel_weights</span> <span class="o">=</span> <span class="n">load_deprel_weights</span><span class="p">(</span><span class="n">weights_file</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">evaluate</span><span class="p">(</span><span class="n">gold_ud</span><span class="p">,</span> <span class="n">system_ud</span><span class="p">,</span> <span class="n">deprel_weights</span><span class="p">)</span></div>


<div class="viewcode-block" id="run_conllu_eval"><a class="viewcode-back" href="../../../../../../generated_api/nlp_architect.models.bist.eval.conllu.html#nlp_architect.models.bist.eval.conllu.conll17_ud_eval.run_conllu_eval">[docs]</a><span class="k">def</span> <span class="nf">run_conllu_eval</span><span class="p">(</span><span class="n">gold_file</span><span class="p">,</span> <span class="n">test_file</span><span class="p">,</span> <span class="n">weights_file</span><span class="o">=</span><span class="n">WEIGHTS</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="c1"># Use verbose if weights are supplied</span>
    <span class="k">if</span> <span class="n">weights_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">verbose</span><span class="p">:</span>
        <span class="n">verbose</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="c1"># Evaluate</span>
    <span class="n">evaluation</span> <span class="o">=</span> <span class="n">evaluate_wrapper</span><span class="p">(</span><span class="n">gold_file</span><span class="p">,</span> <span class="n">test_file</span><span class="p">,</span> <span class="n">weights_file</span><span class="p">)</span>

    <span class="c1"># Write the evaluation to file</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">test_file</span><span class="p">[:</span> <span class="n">test_file</span><span class="o">.</span><span class="n">rindex</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)]</span> <span class="o">+</span> <span class="s2">&quot;_eval.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">out_file</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">verbose</span><span class="p">:</span>
            <span class="n">out_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;LAS F1 Score: </span><span class="si">{:.2f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">100</span> <span class="o">*</span> <span class="n">evaluation</span><span class="p">[</span><span class="s2">&quot;LAS&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">f1</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">metrics</span> <span class="o">=</span> <span class="p">[</span>
                <span class="s2">&quot;Tokens&quot;</span><span class="p">,</span>
                <span class="s2">&quot;Sentences&quot;</span><span class="p">,</span>
                <span class="s2">&quot;Words&quot;</span><span class="p">,</span>
                <span class="s2">&quot;UPOS&quot;</span><span class="p">,</span>
                <span class="s2">&quot;XPOS&quot;</span><span class="p">,</span>
                <span class="s2">&quot;Feats&quot;</span><span class="p">,</span>
                <span class="s2">&quot;AllTags&quot;</span><span class="p">,</span>
                <span class="s2">&quot;Lemmas&quot;</span><span class="p">,</span>
                <span class="s2">&quot;UAS&quot;</span><span class="p">,</span>
                <span class="s2">&quot;LAS&quot;</span><span class="p">,</span>
            <span class="p">]</span>
            <span class="k">if</span> <span class="n">weights_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">metrics</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;WeightedLAS&quot;</span><span class="p">)</span>

            <span class="n">out_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;Metrics    | Precision |    Recall |  F1 Score | AligndAcc&quot;</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">out_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;-----------+-----------+-----------+-----------+-----------&quot;</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span> <span class="n">metrics</span><span class="p">:</span>
                <span class="n">out_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
                    <span class="s2">&quot;</span><span class="si">{:11}</span><span class="s2">|</span><span class="si">{:10.2f}</span><span class="s2"> |</span><span class="si">{:10.2f}</span><span class="s2"> |</span><span class="si">{:10.2f}</span><span class="s2"> |</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">metric</span><span class="p">,</span>
                        <span class="mi">100</span> <span class="o">*</span> <span class="n">evaluation</span><span class="p">[</span><span class="n">metric</span><span class="p">]</span><span class="o">.</span><span class="n">precision</span><span class="p">,</span>
                        <span class="mi">100</span> <span class="o">*</span> <span class="n">evaluation</span><span class="p">[</span><span class="n">metric</span><span class="p">]</span><span class="o">.</span><span class="n">recall</span><span class="p">,</span>
                        <span class="mi">100</span> <span class="o">*</span> <span class="n">evaluation</span><span class="p">[</span><span class="n">metric</span><span class="p">]</span><span class="o">.</span><span class="n">f1</span><span class="p">,</span>
                        <span class="s2">&quot;</span><span class="si">{:10.2f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">100</span> <span class="o">*</span> <span class="n">evaluation</span><span class="p">[</span><span class="n">metric</span><span class="p">]</span><span class="o">.</span><span class="n">aligned_accuracy</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">evaluation</span><span class="p">[</span><span class="n">metric</span><span class="p">]</span><span class="o">.</span><span class="n">aligned_accuracy</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
                        <span class="k">else</span> <span class="s2">&quot;&quot;</span><span class="p">,</span>
                    <span class="p">)</span>
                    <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span>
                <span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>